/*
 * libustream: User-space stackable streams
 * @(#) $Id: module.c 2 2009-02-20 09:52:11Z mo@ilaven.net $
 */

/*
 * Copyright (c) 2002, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_libustream.h"

typedef struct us_module_struct us_module_t;

struct us_module_struct
{
	char scheme[32];
	com_clsid_t clsid;
};

static us_module_t *modules;
static size_t nmodules;
static int modinit;
static pthread_mutex_t modlock = PTHREAD_MUTEX_INITIALIZER;

static void us__module_init(void);

int
us__module_lookup(const char *scheme, com_clsid_t *clsid)
{
	size_t c;
	
	pthread_mutex_lock(&modlock);
	if(0 == modinit)
	{
		us__module_init();
	}
	for(c = 0; c < nmodules; c++)
	{
		if(0 == strcmp(modules[c].scheme, scheme))
		{
			memcpy(clsid, &(modules[c].clsid), sizeof(com_clsid_t));
			pthread_mutex_unlock(&modlock);
			return 0;
		}
	}
	pthread_mutex_unlock(&modlock);
	return -1;
}

/* Register the class GUID @clsid as being the handler for the scheme @scheme */
int
us_module_register(const char *scheme, const com_rclsid_t clsid)
{
	us_module_t *q;
	
	pthread_mutex_lock(&modlock);
	if(0 == modinit)
	{
		us__module_init();
	}
	if(NULL == (q = (us_module_t *) realloc(modules, sizeof(us_module_t) * (nmodules + 1))))
	{
		pthread_mutex_unlock(&modlock);
		return -1;
	}
	strncpy(q[nmodules].scheme, scheme, sizeof(q[nmodules].scheme) - 1);
	memcpy(&(q[nmodules].clsid), clsid, sizeof(com_clsid_t));
	modules = q;
	nmodules++;
	pthread_mutex_unlock(&modlock);
	return 0;
}


static void
us__module_init(void)
{
	/* We need to release the mutex to prevent a deadlock when 
	 * us__module_register_builtin attempts to register the built-in class
	 * GUIDs.
	 * XXX: There's a race here still, though non-fatal. Builtins may not be
	 * registered by the time a second thread comes to hold the lock
	 * and starts looking up modules.
	 */
	modinit = 1;
	pthread_mutex_unlock(&modlock);
	us__module_register_builtin();
	pthread_mutex_lock(&modlock);
}
